package org.jim.server.adapter;

import org.jim.core.ImChannelContext;
import org.jim.core.packets.Command;
import org.jim.core.packets.Message;
import org.jim.core.utils.JsonKit;

/**
 * 消息转换器Demo
 */
public class DemoMessageConverter extends BaseMessageConverter {
    public static final Command COMMAND_DEMO = Command.addAndGet("COMMAND_DEMO", 9999);
    public static class DemoMessage extends Message{
        private Integer demo;
        public Integer getDemo() {
            return demo;
        }
    }
    @Override
    public boolean supports(byte[] body) {
        try {
            // 尝试直接解析为Message对象判断是否支持
            DemoMessage message = JsonKit.toBean(body, DemoMessage.class);
            return message.getDemo() != null ? true :false;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public DemoMessage convert(byte[] body, ImChannelContext channelContext) {
        DemoMessage message = JsonKit.toBean(body, DemoMessage.class);
        return message;
    }
}
